package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfAttachFileService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.common.enums.trade.status.AttachFileStatusEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.QueryAttachListByNoAndTypeListBo;
import com.mdb.open.trade.domain.bo.trade.QueryAttachListByNoListBo;
import com.mdb.open.trade.domain.bo.trade.SaveAttachFileBo;
import com.mdb.open.trade.domain.dto.trade.UpdateAttachFileDto;
import com.mdb.open.trade.domain.po.trade.AttachFile;
import com.mdb.open.trade.domain.vo.trade.AttachNoVo;
import com.mdb.open.trade.mapper.trade.TfAttachFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *  融资申请附件表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfAttachFileService extends SuperService<TfAttachFileMapper, AttachFile> {

    @Autowired
    SequenceGenerator sequenceGenerator;

    public ResultEx<AttachNoVo> saveAttachFile(SaveAttachFileBo bo) {

        AttachFile file = new AttachFile();
        //编号生成
        String attachNo = sequenceGenerator.generator(TfSequenceEnum.ATTACH_NO);
        file.setCustomerCode(bo.getCustomerCode());
        file.setMerchantNo(bo.getMerchantNo());
        file.setAttachNo(attachNo);
        file.setApplyOrderNo(bo.getApplyOrderNo());
        file.setBizOrderNo(bo.getBizOrderNo());
        file.setType(bo.getType());
        file.setFileUrl(bo.getFileNo());
        file.setFileName(bo.getFileName());
        file.setStatus(AttachFileStatusEnum.NORMAL);

        if (this.baseMapper.insert(file) != 1) {
            return ResultEx.fail();
        }

        AttachNoVo noDto = new AttachNoVo();
        noDto.setAttachNo(attachNo);

        return ResultEx.ok(noDto);
    }

    public ResultEx updateAttachFile(UpdateAttachFileDto dto) {

        AttachFile file = this.baseMapper.selectOne(new LambdaUpdateWrapper<AttachFile>().eq(AttachFile::getAttachNo, dto.getAttachNo()));
        if (ObjectUtil.isEmpty(file)) {
            return ResultEx.fail();
        }

        LambdaUpdateWrapper<AttachFile> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AttachFile::getAttachNo, dto.getAttachNo());
        if (this.baseMapper.delete(queryWrapper) != 1) {
            return ResultEx.fail();
        }

        AttachFile addFile = new AttachFile();
        //编号生成
        addFile.setAttachNo(sequenceGenerator.generator(TfSequenceEnum.ATTACH_NO));
        addFile.setApplyOrderNo(file.getApplyOrderNo());
        addFile.setBizOrderNo(file.getBizOrderNo());
        addFile.setCustomerCode(file.getCustomerCode());
        addFile.setMerchantNo(file.getMerchantNo());
        addFile.setFileUrl(dto.getFileNo());
        addFile.setType(file.getType());
        addFile.setStatus(AttachFileStatusEnum.NORMAL);

        if (this.baseMapper.insert(addFile) != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    public ResultEx removeAttachFile(String attachNo) {

        LambdaUpdateWrapper<AttachFile> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AttachFile::getAttachNo, attachNo);
        if (this.baseMapper.delete(queryWrapper) != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    /**
     * 根据业务单号列表和类型列表查询附件列表
     * @param bo
     * @return
     */
    public List<AttachFile> queryAttachByNoAndTypeList(QueryAttachListByNoAndTypeListBo bo) {

        LambdaQueryWrapper<AttachFile> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bo.getApplyOrderNo())) {
            queryWrapper.eq(AttachFile::getApplyOrderNo, bo.getApplyOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getStatus())) {
            queryWrapper.in(AttachFile::getStatus, bo.getStatus());
        }
        if (ObjectUtil.isNotEmpty(bo.getBizOrderNo())) {
            queryWrapper.in(AttachFile::getBizOrderNo, bo.getBizOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getType())) {
            queryWrapper.in(AttachFile::getType, bo.getType());
        }

        queryWrapper.orderByDesc(AttachFile::getId);
        queryWrapper.eq(AttachFile::isDeleted, false);

        return this.baseMapper.selectList(queryWrapper);
    }

    public ResultEx batchRemoveAttachFile(List<String> attachNoList) {

        for (String attachNo : attachNoList) {
            LambdaUpdateWrapper<AttachFile> queryWrapper = new LambdaUpdateWrapper<>();
            queryWrapper.eq(AttachFile::getAttachNo, attachNo);
            this.baseMapper.delete(queryWrapper);
        }
        return ResultEx.ok();
    }

    /**
     * 根据业务单号列表查询附件列表
     * @param bo
     * @return
     */
    public List<AttachFile> queryAttachByNoList(QueryAttachListByNoListBo bo) {

        LambdaQueryWrapper<AttachFile> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bo.getApplyOrderNo())) {
            queryWrapper.eq(AttachFile::getApplyOrderNo, bo.getApplyOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getBizOrderNo())) {
            queryWrapper.in(AttachFile::getBizOrderNo, bo.getBizOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getStatus())) {
            queryWrapper.eq(AttachFile::getStatus, bo.getStatus());
        }
        if (ObjectUtil.isNotEmpty(bo.getType())) {
            queryWrapper.eq(AttachFile::getType, bo.getType());
        }
        queryWrapper.eq(AttachFile::isDeleted, false);
        queryWrapper.orderByDesc(AttachFile::getId);

        return this.baseMapper.selectList(queryWrapper);

    }
}
